
package me.module.toolMain
{
	import com.bit101.components.CheckBox;
	import com.bit101.components.ColorChooser;
	import com.bit101.components.HUISlider;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.List;
	import com.bit101.components.Panel;
	import com.bit101.components.PushButton;
	import com.bit101.components.VBox;
	import com.bit101.components.Window;
	import com.zero.framework.interfaces.INotification;
	import com.zero.framework.interfaces.INotifyResponder;
	import com.zero.util.InstanceUtil;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import me.common.AppFacade;
	import me.common.Common;
	import me.common.GlobalCore;
	import me.module.toolMain.mainElement.ActionWindow;
	import me.module.toolMain.mainElement.BitmapPlayer;
	import me.notification.MainNotification;
	import me.vo.ActionVO;
	
	/**
	 * @author 缺硒
	 */
	public class MainView extends Window implements INotifyResponder
	{
		private var _actionWin:ActionWindow;
		private var _findPathBtn:PushButton;
		private var _outPutBtn:PushButton;
		private var _hLabel:Label;
		private var _wLabel:Label;
		private var _wInput:InputText;
		private var _hInput:InputText;
		private var _preHLabel:Label;
		private var _preWLabel:Label;
		private var _preHInput:InputText;
		private var _preWInput:InputText;
		private var _offsetXLabel:Label;
		private var _offsetYLabel:Label;
		private var _offsetXInput:InputText;
		private var _offsetYInput:InputText;
		private var _setSizeBtn:PushButton;
		private var _setOffsetBtn:PushButton;
		private var _actionList:List;
		private var _frameLabel:Label;
		private var _stopBtn:PushButton;
		private var _playBtn:PushButton;
		private var _frameRateLabel:Label;
		private var _frameRateSlider:HUISlider;
		private var _configPanel:Panel;
		private var _locatePointLabel:Label;
		
		// config uis
		private var _cFNumLabel:Label;
		private var _cRDirLabel:Label;
		private var _cWDirLabel:Label;
		private var _cFrameInput:InputText;
		private var _cRunDirInput:InputText;
		private var _cWaitDirInput:InputText;
		
		private var _offsetX:Number;
		private var _offsetY:Number;
		
		private var _isNpcCheckBox:CheckBox;
		private var _colorSelecter:ColorChooser;
		private var _locatePoint:Sprite;
		
		private var _rightBox:Sprite;
		private var _buttonBox:Sprite;
		
		// 构造函数
		public function MainView()
		{
			setSize(800, 600);
			initComponents();
			AppFacade.getInstance().registerResponder(this);
			title = "行走场景工具";
			
			_locatePoint = new Sprite();
			_locatePoint.graphics.beginFill(0xff0000,0.4);
			_locatePoint.graphics.drawCircle(0,0, 10);
			_locatePoint.graphics.beginFill(0x0000ff,1);
			_locatePoint.graphics.drawCircle(0,0, 2);
			_locatePoint.graphics.endFill();
			_locatePoint.addEventListener(MouseEvent.MOUSE_DOWN, onLocateDown);
			_locatePoint.addEventListener(MouseEvent.MOUSE_UP, onLocateUp);
			_locatePoint.x = 20;
			_locatePoint.y = 20;
			addChild(_locatePoint);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}		
		
		public function get notificationList():Array
		{
			return [
				MainNotification.TO_SHOW_ACTION_SPRITE
			];
		}
		
		public function notifyHandler(notification:INotification):void
		{
			var body:Object = notification.getBody();
			switch(notification.getName())
			{
				case MainNotification.TO_SHOW_ACTION_SPRITE:
					_actionWin.addAnimator();
					_preHInput.text = GlobalCore.spritePreH.toString();
					_preWInput.text = GlobalCore.spritePreW.toString();
					
					_hInput.text = _preHInput.text;
					_wInput.text = _preWInput.text;
					
					_actionWin.drawWin(int(_wInput.text), int(_hInput.text));
					var listArray:Array = new Array();
					_actionList.removeAll();
					var actions:Array = GlobalCore.actions;
					
					for(var i:int = 0; i < actions.length; i++)
					{
						var action:ActionVO = actions[i];
						for(var j:int = 0; j < action.lineNum; j ++)
						{
							_actionList.addItem({label:action.name + ": " + (j+1), value:{i:i, j:j}});
						}
						
					}
					_actionList.addEventListener(MouseEvent.CLICK, uiActionList);
					uiRealin();
					break;
			}
		}
		
		//初始化组建
		private function initComponents():void
		{
			this.draggable = false;
			
			_rightBox = new Sprite();
			_buttonBox = new Sprite();

			
			_actionWin = InstanceUtil.create(new ActionWindow(), this, {x:40, y:30});
			
			addChild(_rightBox);
			addChild(_buttonBox);
			
			_findPathBtn = new PushButton(_buttonBox, 40, 250, "Find Path", uiFindPath);
			_findPathBtn.width = 90;
			_outPutBtn = new PushButton(_buttonBox, 150, 250, "Out put", uiOutPut);
			_outPutBtn.width = 90;
			
			_hLabel = new Label(_rightBox, 250, 100, "Height:");
			
			_wLabel = new Label(_rightBox, 250, 80, "Width:");
			
			_wInput = new InputText(_rightBox, 320, 80, "");
			_wInput.width = 55;
			
			_hInput = new InputText(_rightBox, 320, 100, "");
			_hInput.width = 55;
			
			_preHLabel = new Label(_rightBox, 250, 50, "Pre Height:");
			
			_preWLabel = new Label(_rightBox, 250, 30, "Pre Width:");
			
			_preHInput = new InputText(_rightBox, 320, 50, "");
			_preHInput.width = 55;
			_preHInput.enabled = false;
			
			_preWInput = new InputText(_rightBox, 320, 30, "");
			_preWInput.width = 55;
			_preWInput.enabled = false;
			
			_setSizeBtn = new PushButton(_rightBox, 390, 100, "Set Size", uiSetSize);
			_setSizeBtn.width = 60;
			_setSizeBtn.height = 16;
			
			_offsetXLabel = new Label(_rightBox, 250, 130, "OffsetX:");
			_offsetYLabel = new Label(_rightBox, 250, 150, "OffsetY:");
			
			_offsetXInput = new InputText(_rightBox, 320, 130, "");
			_offsetXInput.width = 55;
			
			_offsetYInput = new InputText(_rightBox, 320, 150, "");
			_offsetYInput.width = 55;
			
			_setOffsetBtn = new PushButton(_rightBox, 390, 150, "Set Offset", uisetOffset);
			_setOffsetBtn.width = 60;
			_setOffsetBtn.height = 16;
			
			_actionList = new List(_rightBox, 250, 180);
			_actionList.width = 125;
			_actionList.height = 90;
			
			_frameLabel = new Label(this, 40, 10);
			_stopBtn = new PushButton(_rightBox, 390, 254, "Stop", uiStop);
			_stopBtn.setSize(60,16);
			_playBtn = new PushButton(_rightBox, 390, 230, "Play", uiPlay);
			_playBtn.setSize(60,16);
			
			_frameRateSlider = new HUISlider(_buttonBox, 40, 290, "FrameRate", uiFrameSlider);
			_frameRateSlider.maximum = 24;
			_frameRateSlider.minimum = 3;
			_frameRateSlider.tick = 3;
			_frameRateSlider.value = 24;
			
			// config uis
			var configLabel:Label = new Label(_rightBox, 500, 10, "Configs:");
			_configPanel = new Panel(_rightBox, 500, 30);
			_configPanel.setSize(200,100);
			_configPanel.color = 0xeeeeee;
			
			_cFNumLabel = new Label(_configPanel, 10, 10, "Frame Number:");
			
			_cRDirLabel = new Label(_configPanel, 10, 50, "Run Direct Number:");
			
			_cWDirLabel = new Label(_configPanel, 10, 70, "Wait Direct Number:");
			
			_cFrameInput = new InputText(_configPanel, 120, 10, GlobalCore.frameNum.toString());
			_cFrameInput.width = 55;
			
			_cRunDirInput = new InputText(_configPanel, 120, 50);
			_cRunDirInput.width = 55;
			
			_cWaitDirInput = new InputText(_configPanel, 120, 70, GlobalCore.waitLine.toString());
			_cWaitDirInput.width = 55;
			
			_isNpcCheckBox = new CheckBox(_rightBox, 250, 290, "是否为NPC", onNpcCheck);
			_isNpcCheckBox.selected = GlobalCore.isNpc;
			
			_colorSelecter = new ColorChooser(_rightBox, 350, 290);
			_colorSelecter.usePopup = true;
			_colorSelecter.addEventListener(Event.CHANGE, onColorChange);
			
			_locatePointLabel =new Label(_rightBox, 500, 180);
		}
		
		// ui
		private function uiRealin():void
		{
			var pw:Number = GlobalCore.spritePreW - 200;
			var ph:Number = GlobalCore.spritePreH - 200;
			
			trace(_actionWin.width, _actionWin.height);
			if(pw < 0) pw = 0;
			if(ph < 0) ph = 0;
			
			_rightBox.x = pw;
			_buttonBox.y = ph;
			
			this.width = pw+750;
			this.height = ph+400;
			
			Common.stage.nativeWindow.width = this.width;
			Common.stage.nativeWindow.height = this.height;
		}
		
		//ef
		private function onEnterFrame(evt:Event):void
		{
			if(_actionWin.offsetX != _offsetX)
			{
				_offsetX = _actionWin.offsetX;
				_offsetXInput.text = _offsetX.toFixed(2);
			}
			
			if(_actionWin.offsetY != _offsetY)
			{
				_offsetY = _actionWin.offsetY;
				_offsetYInput.text = _offsetY.toFixed(2);
			}
			_locatePointLabel.text = "红点位置   X: " +(_locatePoint.x - _actionWin.x) +"  Y: "+(_locatePoint.y-_actionWin.y);
			_frameLabel.text = "Current Frame: " + (_actionWin.currentFrame + 1).toString();
		}
		
		//点击寻找路径按钮
		private function uiFindPath(evt:Event):void
		{
			AppFacade.getInstance().sendNotification(MainNotification.UI_CLICK_FIND_PATH);
		}
		
		//点击输出png
		private function uiOutPut(evt:Event):void
		{
			AppFacade.getInstance().sendNotification(MainNotification.UI_OUT_PUT,
				{
					w:int(_wInput.text),
					h:int(_hInput.text),
					x:Number(_offsetXInput.text),
					y:Number(_offsetYInput.text)
				});
		}
		
		private function onLocateDown(evt:MouseEvent):void
		{
			_locatePoint.startDrag();
		}
		
		private function onLocateUp(evt:MouseEvent):void
		{
			_locatePoint.stopDrag();
		}
		
		//点击设置长宽按钮
		private function uiSetSize(evt:Event):void
		{
			var w:int = int(_wInput.text);
			var h:int = int(_hInput.text);
			
			_actionWin.drawWin(w, h);
			AppFacade.getInstance().sendNotification(MainNotification.UI_SET_SPRITE_SIZE,
				{
					w:w,
					h:h
				});
		}
		
		//点击设置偏移量按钮
		private function uisetOffset(evt:Event):void
		{
			_offsetX = Number(_offsetXInput.text);
			_offsetY = Number(_offsetYInput.text);
			
			_actionWin.offsetX = _offsetX;
			_actionWin.offsetY = _offsetY;
			
			AppFacade.getInstance().sendNotification(MainNotification.UI_SET_SPRITE_OFFSET,
				{
					x:_offsetX,
					y:_offsetY
				});
		}
		
		//点击动作选择列表
		private function uiActionList(evt:MouseEvent):void
		{
			if(_actionList.selectedItem != null)
			{
				trace(_actionList.selectedItem.value.i, _actionList.selectedItem.value.j)
				_actionWin.action = GlobalCore.actions[_actionList.selectedItem.value.i];
				_actionWin.line = _actionList.selectedItem.value.j;
			}
		}
		
		//点击停止
		private function uiStop(evt:Event):void
		{
			_actionWin.stop();
		}
		
		//点击播放
		private function uiPlay(evt:Event):void
		{
			_actionWin.play();
		}
		
		private function uiFrameSlider(evt:Event):void
		{
			BitmapPlayer.playDely = int(24/_frameRateSlider.value);
		}
		
		//是否npc被点击
		private function onNpcCheck(e:Event):void
		{
			GlobalCore.isNpc = _isNpcCheckBox.selected;
		}
		
		private function onColorChange(e:Event):void
		{
			_actionWin.backgroundColor = _colorSelecter.value;
			var w:int = int(_wInput.text);
			var h:int = int(_hInput.text);
			_actionWin.drawWin(w, h);
		}
		//
	}
}

